package com.oa.official.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oa.official.entity.process.Process;
import com.oa.official.entity.process.ProcessTemplate;
import com.oa.official.entity.system.SysUser;
import com.oa.official.mapper.SysUserMapper;
import com.oa.official.service.MessageService;
import com.oa.official.service.OaProcessService;
import com.oa.official.service.OaProcessTemplateService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;
@Slf4j
@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private OaProcessService processService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private OaProcessTemplateService processTemplateService;

    @Resource
    private WxMpService wxMpService;

    @Value(value = "wechat.messageTemplateId")
    private String messageTemplateId;

    //推送待审批人员
    @Override
    public void pushPendingMessage(Long processId, Long userId, String taskId) {
        //查询流程信息
        Process process = processService.getById(processId);
        //根据userid查询要推送人信息
        SysUser sysUser = sysUserMapper.selectById(userId);
        //查询审批模板信息
        ProcessTemplate processTemplate = processTemplateService.getById(process.getProcessTemplateId());
        //获取提交审批人的信息
        SysUser submitSysUser = sysUserMapper.selectById(process.getUserId());

        //获取要给的消息人的openid
        String openId = sysUser.getOpenId();
        if(StringUtils.hasText(openId)) {
            //设置消息发送信息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    //给谁发送消息，openid值
                    .toUser(openId)
                    //创建模板信息的id值
                    .templateId(messageTemplateId)
                    //点击消息，跳转的地址
                    .url("http://weblanqiang.viphk.nnhk.cc/#/show/" + processId + "/" + taskId)
                    .build();

            JSONObject jsonObject = JSON.parseObject(process.getFormValues());
            JSONObject formShowData = jsonObject.getJSONObject("formShowData");
            StringBuilder content = new StringBuilder();
            for (Map.Entry<String,Object> entry : formShowData.entrySet()) {
                content.append(entry.getKey()).append("：").append(entry.getValue()).append("\n ");
            }

            //设置模板里面参数值
            templateMessage.addData(new WxMpTemplateData("first", submitSysUser.getName()+"提交"+processTemplate.getName()+",请注意查看","#272727"));
            templateMessage.addData(new WxMpTemplateData("keyword1", process.getProcessCode(), "#272727"));
            templateMessage.addData(new WxMpTemplateData("keyword2", new DateTime(process.getCreateTime()).toString("yyyy-MM-dd HH:mm:ss"), "#272727"));
            templateMessage.addData(new WxMpTemplateData("content", content.toString(), "#272727"));

            //调用方法发送
            try {
                String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                System.out.println(msg);
                log.info("发送消息成功,msg={}",msg);
            } catch (WxErrorException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
